<script lang="ts" setup>
import { computed, watch, ref, onMounted } from "vue"
import { useRoute } from "vue-router"
import { storeToRefs } from "pinia"
import { useAppStore } from "@/store/modules/app"
import { useSettingsStore } from "@/store/modules/settings"
import { AppMain, Sidebar } from "./components"
import { useDevice } from "@/hooks/useDevice"
import { useLayoutMode } from "@/hooks/useLayoutMode"

const { isMobile } = useDevice()
const appStore = useAppStore()
const settingsStore = useSettingsStore()
const { showTagsView, fixedHeader } = storeToRefs(settingsStore)
const { isTop } = useLayoutMode()
const showSidebar = ref(false)
const route = useRoute()
// 当前页面是否显示面包屑
const isDashboard = ref(false)
watch(
  () => route.path,
  () => {
    // console.log("route", route.name)
    isDashboard.value = route.name === "Dashboard"
  }
)
/** 定义计算属性 layoutClasses，用于控制布局的类名 */
const layoutClasses = computed(() => {
  return {
    hideSidebar: !appStore.sidebar.opened,
    openSidebar: appStore.sidebar.opened,
    withoutAnimation: appStore.sidebar.withoutAnimation,
    mobile: isMobile.value
  }
})

/** 用于处理点击 mobile 端侧边栏遮罩层的事件 */
const handleClickOutside = () => {
  appStore.closeSidebar(false)
}
onMounted(() => {
  isDashboard.value = route.name === "Dashboard"
  if (window.self === window.top) {
    showSidebar.value = true
    const query = route.query
    if (query && query.platform === "map") {
      showSidebar.value = false
    }
  } else {
    showSidebar.value = false
  }
})
</script>

<template>
  <div :class="layoutClasses" class="app-wrapper">
    <!-- mobile 端侧边栏遮罩层 -->
    <div v-if="layoutClasses.mobile && layoutClasses.openSidebar" class="drawer-bg" @click="handleClickOutside" />
    <!-- 左侧边栏 -->
    <Sidebar class="sidebar-container" :class="isDashboard ? 'black-theme' : ''" v-if="showSidebar" />
    <!-- 主容器 -->
    <div :class="{ hasTagsView: showTagsView }" class="main-container">
      <!-- 头部导航栏和标签栏 -->
      <!-- <div :class="{ 'fixed-header': fixedHeader }" class="layout-header">
        <NavigationBar :blackTheme="isDashboard" />
        <TagsView v-if="showTagsView" />
        <Breadcrumb v-if="(!isTop || isMobile ) && !isDashboard" class="breadcrumb" />
      </div> -->
      <!-- 页面主体内容 -->
      <AppMain class="app-main" :blackTheme="isDashboard" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import "@/styles/mixins.scss";
$transition-time: 0.35s;

.app-wrapper {
  @extend %clearfix;
  position: relative;
  width: 100%;
}

.drawer-bg {
  background-color: rgba(0, 0, 0, 0.3);
  width: 100%;
  top: 0;
  height: 100%;
  position: absolute;
  z-index: 999;
}

.sidebar-container {
  background-color: #fff;
  transition: width $transition-time;
  width: 60px !important;
  height: calc(100% - 60px);
  position: fixed;
  top: 30px;
  bottom: 30px;
  left: 10px;
  z-index: 1001;
  overflow: hidden;
  /* border-right: var(--v3-sidebar-border-right); */
  border-radius: 15px;
  box-shadow: 14px 17px 40px 0px rgba(112, 144, 176, 0.08);
  &.black-theme {
    border-radius: 0;
  }
}

.main-container {
  min-height: 100%;
  transition: margin-left $transition-time;
  margin-left: 0px;
  position: relative;
}

.fixed-header {
  position: fixed !important;
  top: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - var(--v3-sidebar-width) + 20px);
  transition: width $transition-time;
}

.layout-header {
  position: relative;
  z-index: 9;
  background-color: var(--v3-header-bg-color);
  /* box-shadow: var(--v3-header-box-shadow);
  border-bottom: var(--v3-header-border-bottom); */
}

.app-main {
  min-height: calc(100vh - var(--v3-navigationbar-height));
  position: relative;
  overflow: hidden;
}

.fixed-header + .app-main {
  /* padding-top: var(--v3-navigationbar-height); */
  padding-top: var(--v3-header-height);
  height: 100vh;
  overflow: auto;
}

.hasTagsView {
  .app-main {
    min-height: calc(100vh - var(--v3-header-height));
  }
  .fixed-header + .app-main {
    padding-top: var(--v3-header-height);
  }
}

.hideSidebar {
  .sidebar-container {
    /* width: var(--v3-sidebar-hide-width) !important; */
    width: calc(var(--v3-sidebar-hide-width) + 20px) !important;
  }
  .main-container {
    /* margin-left: var(--v3-sidebar-hide-width); */
    margin-left: calc(var(--v3-sidebar-hide-width) + 20px);
  }
  .fixed-header {
    width: calc(100% - var(--v3-sidebar-hide-width));
  }
}

// 适配 mobile 端
.mobile {
  .sidebar-container {
    transition: transform $transition-time;
    width: var(--v3-sidebar-width) !important;
  }
  .main-container {
    margin-left: 0px;
  }
  .fixed-header {
    width: 100%;
  }
  &.openSidebar {
    position: fixed;
    top: 0;
  }
  &.hideSidebar {
    .sidebar-container {
      pointer-events: none;
      transition-duration: 0.3s;
      transform: translate3d(calc(0px - var(--v3-sidebar-width)), 0, 0);
    }
  }
}

.withoutAnimation {
  .sidebar-container,
  .main-container {
    transition: none;
  }
}
.breadcrumb {
  flex: 1;
  // 参考 Bootstrap 的响应式设计将宽度设置为 576
  @media screen and (max-width: 576px) {
    display: none;
  }
}
</style>
